//
//  LBCCategoryListCell.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryListCell.h"

@interface LBCCategoryListCell ()

@property (nonatomic, strong)UIImageView *headImageView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UIImageView *playImageView;
@property (nonatomic, strong)UILabel *playCountLable;
@property (nonatomic, strong)UIImageView *allImageView;
@property (nonatomic, strong)UILabel *allCountLabel;

@end

@implementation LBCCategoryListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.headImageView = [UIImageView new];
        _headImageView.backgroundColor = [UIColor purpleColor];
        [self.contentView addSubview:_headImageView];
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
            make.width.equalTo(_headImageView.mas_height);
            
        }];
        
        self.titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:19];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headImageView);
            make.left.equalTo(_headImageView.mas_right).offset(10);
            make.right.mas_equalTo(-SCREEN_WIDTH / 22);
            
        }];
        
        self.contentLabel = [UILabel new];
        _contentLabel.textColor = [UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];
        _contentLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.width.equalTo(_titleLabel);
            make.top.equalTo(_titleLabel.mas_bottom).offset(5);
            
        }];

        self.playImageView = [UIImageView new];
        _playImageView.image = [UIImage imageNamed:@"sound_playtimes@2x.png"];
        [self.contentView addSubview:_playImageView];
        [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_contentLabel.mas_bottom).offset(8);
            make.left.equalTo(_titleLabel);
            make.bottom.equalTo(_headImageView);
            make.width.equalTo(_playImageView.mas_height);
            
        }];

        self.playCountLable = [UILabel new];
        _playCountLable.textColor = [UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];
        _playCountLable.font = [UIFont systemFontOfSize:13];
        // 计算文字大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGFloat length = [_playCountLable.text boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        [self.contentView addSubview:_playCountLable];
        [_playCountLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_playImageView);
            make.bottom.equalTo(_playImageView);
            make.left.equalTo(_playImageView.mas_right).offset(2);
            make.width.offset(length);
            
        }];
        
        self.allImageView = [UIImageView new];
        _allImageView.image = [UIImage imageNamed:@"album_tracks@2x.png"];
        [self.contentView addSubview:_allImageView];
        [_allImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_playCountLable.mas_right).offset(5);
            make.top.bottom.equalTo(_playCountLable);
            make.width.equalTo(_allImageView.mas_height);
            
        }];

        self.allCountLabel = [UILabel new];
        _allCountLabel.textColor = [UIColor colorWithRed:0.64 green:0.64 blue:0.64 alpha:1];
        _allCountLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:_allCountLabel];
        [_allCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_allImageView.mas_right).offset(2);
            make.top.bottom.equalTo(_allImageView);
            make.right.equalTo(_contentLabel);
            
        }];
        
        UIView *lineV = [UIView new];
        lineV.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
        [self.contentView addSubview:lineV];
        [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLabel);
            make.bottom.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.height.mas_equalTo(1);
            
        }];

        
    }
    return self;
}


-(void)setCateListModel:(LBCCategoryListModel *)cateListModel{
    _cateListModel = cateListModel;
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:_cateListModel.albumCoverUrl290] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
    _titleLabel.text = _cateListModel.title;
    _contentLabel.text = _cateListModel.intro;
    _allCountLabel.text = [NSString stringWithFormat:@"%ld集", _cateListModel.tracks];
    if (_cateListModel.playsCounts < 10000) {
        _playCountLable.text = [NSString stringWithFormat:@"%ld", _cateListModel.playsCounts];
    } else {
    _playCountLable.text = [NSString stringWithFormat:@"%.1f万", _cateListModel.playsCounts / 10000.0];
    }
    // 计算文字大小
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
    CGFloat length = [_playCountLable.text boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    [_playCountLable mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(length + 1);
        
    }];

}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
